package club.dmyang.controller;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.vo.ResposeVo;
import club.dmyang.entity.FriendShipLink;
import club.dmyang.service.FriendshipLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by brss on 2018/8/16.
 */
@Controller
@RequestMapping("/friendshiplink")
public class FriendshipLinkController {

    @Autowired
    private FriendshipLinkService friendshipLinkService;

    /**
     * 列出所有友链
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResposeVo list(){
        List<FriendShipLink> friendShipLinkList = friendshipLinkService.findAllFriendshipLink();
        return new ResposeVo(0,"成功",friendShipLinkList.size(),friendShipLinkList);
    }

    /**
     * 跳转到编辑页面（填充响应表单）
     * @param flid
     * @param model
     * @return
     */
    @RequestMapping("/edit/{flid}")
    public String edit(@PathVariable("flid") Integer flid, Model model){
        FriendShipLink friendshipLink = friendshipLinkService.findOneFriendshipLink(flid);

        model.addAttribute("friendshipLink",friendshipLink);
        return "admin/friendshiplink-add";
    }

    /**
     * 更新
     * @param friendShipLink
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResposeVo update(FriendShipLink friendShipLink){
        boolean b = friendshipLinkService.changeOneFriendshipLink(friendShipLink);
        if (b)
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),"更新友链成功");
        else
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(),"更新友链失败");
    }

    /**
     * 删除
     * @param flid
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete/{flid}")
    public ResposeVo delete(@PathVariable("flid") Integer flid){
        boolean b = friendshipLinkService.delOneFriendshipLink(flid);
        if (b)
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),"删除友链成功");
        else
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(),"删除友链失败");
    }

    /**
     * 添加
     * @param friendShipLink
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public ResposeVo add(FriendShipLink friendShipLink){
        boolean b = friendshipLinkService.addOneFriendshipLink(friendShipLink);
        if (b)
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),"添加友链成功");
        else
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(),"添加友链失败");
    }
}
